﻿Imports System.Security.Cryptography

Public Class clsEncriptador
#Region "PROPIEDADES"
    Private propCadena As String
    Private propClave As String
#End Region

#Region "METODOS DE ACCESO A LAS PROPIEDADES"
    ''' <summary>
    ''' Cadena de texto que se requiere Encriptar o Desencriptar
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Cadena() As String
        Get
            Return Me.propCadena
        End Get
        Set(ByVal value As String)
            Me.propCadena = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Clave para encritar o desencriptar la cadena
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Clave() As String
        Get
            Return Me.propClave
        End Get
        Set(ByVal value As String)
            Me.propClave = value.Trim
        End Set
    End Property
#End Region

#Region "FUNCIONES DE PROPIEDADES"
    ''' <summary>
    ''' Encripta la cadena configurada según la clave dada
    ''' </summary>
    ''' <returns>STRING</returns>
    ''' <remarks></remarks>
    Public Function EncriptarCadena() As String
        Dim strResultado As String = "ERROR"
        Try
            strResultado = Encripta()
        Catch ex As Exception
        End Try
        Return strResultado
    End Function

    ''' <summary>
    ''' Desencripta la cadena configurada según la clave entregada
    ''' </summary>
    ''' <returns>STRING</returns>
    ''' <remarks></remarks>
    Public Function DesencriptarCadena() As String
        Dim strResultado As String = "ERROR"
        Try
            strResultado = DesEncripta()
        Catch ex As Exception
        End Try
        Return strResultado
    End Function

    ''' <summary>
    ''' Elimina desde la memoria el objeto
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Dispose()
        Try
            GC.SuppressFinalize(Me)
        Catch ex As Exception
        End Try
    End Sub
#End Region

#Region "FUNCIONES DE USO INTERNO"
    Private Function Encripta() As String
        Dim i As Integer, Pass2 As String
        Dim CAR As String, Codigo As String
        Pass2 = ""
        For i = 1 To Len(Me.propCadena)
            CAR = Mid(Me.propCadena, i, 1)
            Codigo = Mid(Me.propClave, ((i - 1) Mod Len(Me.propClave)) + 1, 1)
            Pass2 = Pass2 & Microsoft.VisualBasic.Strings.Right("0" & Hex(Asc(Codigo) Xor Asc(CAR)), 2)
        Next i
        Encripta = Pass2
    End Function

    Private Function DesEncripta() As String
        Dim i As Integer, Pass2 As String
        Dim CAR As String, Codigo As String
        Dim j As Integer
        Pass2 = ""
        j = 1
        For i = 1 To Len(Me.propCadena) Step 2
            CAR = Mid(Me.propCadena, i, 2)
            Codigo = Mid(Me.propClave, ((j - 1) Mod Len(Me.propClave)) + 1, 1)
            Pass2 = Pass2 & Chr(Asc(Codigo) Xor Val("&h" + CAR))
            j = j + 1
        Next i
        DesEncripta = Pass2
    End Function
#End Region
End Class
